
Page({
  data:{
    item:0,
    state:'paused',
    music:[
      {
        id:1,
        name:'孤勇者',
        author:'陈奕迅',
        src:'http://m701.music.126.net/20220829163948/f8a92a858813cc784b2bf805acf7f1cd/jdymusic/obj/wo3DlMOGwrbDjj7DisKw/14096429529/2bea/211b/0672/43be1ec076b8d7d7dc457549274e1d5f.mp3'
      },
      {
        id:2,
        name:'在你的身边',
        author:'盛哲',
        src:'http://m701.music.126.net/20220829164101/b287be19123dcc16a8eb5fc0e18a6a54/jdymusic/obj/wo3DlMOGwrbDjj7DisKw/14096410711/7e04/100d/d588/25b0beab3b8bacaa87e4c5fc8eea6cf2.mp3'
      },
      {
        id:3,
        name:'Sweet Dream',
        author:'Breathe Carolina',
        src:'http://m802.music.126.net/20220829164201/4d9ee1c73f5e04396445aaeb2adbd3c1/jd-musicrep-ts/5cbd/1832/e425/6b12b1f5b5c628438b3a0f3a703aef24.mp3'
      }
    ],
    plays:{
      currentTime:'',
      duration:'',
      percent:'',
      name:'',
      author:''
    },
    musics:0
  },
  // swiper改变事件
  change:function(e){
    this.setData({
      item:e.target.dataset.item
    })
  },
  // 改变tag事件
  changetag:function(e){
    this.setData({
      item:e.detail.current
    })
  },
  // 播放列表点击换歌
  list_change:function(e){
    this.setmusic(e.currentTarget.dataset.index)
    this.running()
  },
  // 播放列表事件
  ms_list:function(e){
    this.setData({
      item:2
    })
  },
  // 实例化音乐API
onReady: function (e) {
  // 实例化播放API
  this.audioCtx = wx.createInnerAudioContext()
  var that=this
  // 播放完自动下一曲
  this.audioCtx.onEnded(function(){
    that.ms_next()
  })
  // 歌曲播放时刷新进度
  this.audioCtx.onTimeUpdate(function(){
    that.setData({
      'plays.duration':settime(that.audioCtx.duration),
      'plays.currentTime':settime(that.audioCtx.currentTime),
      'plays.percent':that.audioCtx.currentTime/that.audioCtx.duration*100
    })
  })
  // 默认第二首
  this.setmusic(1)
  // 格式化时间函数
  function settime(time){
    var m=Math.floor(time/60)%60
    var s=Math.floor(time)%60
    return (m<10?'0'+m:m)+':'+(s<10?'0'+s:s)
  }
},
// 播放进度改变时触发函数
mschange:function(e){
  // 讲进度条转化成歌曲指定长度
  var c=e.detail.value*this.audioCtx.duration /100
  // 跳转到指定位置
  this.audioCtx.seek(c)
},
// 设置播放音乐函数
  setmusic:function(index){
    var music=this.data.music[index]
    this.audioCtx.src=music.src
    if(this.data.state=='running'){
      this.audioCtx.play()
    }
    this.setData({
      musics:index,
      'plays.name':music.name,
      'plays.author':music.author,
      'plays.currentTime':'00:00',
      'plays.duration':'00:00',
      'plays.percent':0,
    })
  },
  // 播放事件
  running:function(e){
    this.setData({
      state:'running'
    })
    this.audioCtx.play()
  },
  // 暂停事件
  paused:function(e){
    this.setData({
      state:'paused'
    })
    this.audioCtx.pause()
  },
  // 下一首事件
  ms_next:function(e){
    this.setData({
      musics: (this.data.musics===this.data.music.length-1)?0:(this.data.musics+1)
    })
    this.setmusic(this.data.musics)
    if(this.data.state=='paused'){
      this.running()
    }
  },
  // 播放栏点击跳转事件
  plays:function(e){
    this.setData({
      item:1
    })
  }
})

